package com.baijiaoxi.utils.sms.tencent;

import com.baijiaoxi.common.model.base.CommonError;
import com.baijiaoxi.common.model.base.CommonResponse;
import com.baijiaoxi.common.model.base.CoreException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;

public class TencentSmsHelper {

    private int appId;
    private String appKey;
    private String sign;

    public TencentSmsHelper(int appId, String appKey, String sign) {
        this.appId = appId;
        this.appKey = appKey;
        this.sign = sign;
    }

    public CommonResponse sendSMS(int templateId, String phoneNumber, String[] params) {

        try {
            //params数组具体的元素个数和模板中变量个数必须一致，例如事例中templateId:5678对应一个变量，参数数组中元素个数也必须是一个
            SmsSingleSender sender = new SmsSingleSender(this.appId, this.appKey);
            SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber,
                    templateId, params, sign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            if (result.result == 0)
                return CommonResponse.of(result);
            else return CommonResponse.ofFail(result);
        } catch (Exception e) {
            throw CoreException.create(CommonError.smsSendFail, e);
        }

    }

}
